import models from '../../../models'
import bcrypt from 'bcrypt'

export default defineEventHandler(async (event: any) => {
  try {
    const id = event.context.params.id
    const body = await readBody(event)
    const { email, password } = body

    const updateData: { email: string; password?: string } = { email }
    if (password) {
      updateData.password = await bcrypt.hash(password, 10)
    }
    
    await models.User.update(updateData, {
      where: { id }
    })
    
    const user = await models.User.findByPk(id)
    
    return {
      success: true,
      user: {
        id: user.id,
        email: user.email
      }
    }
  } catch (error) {
    throw createError({
      statusCode: 500,
      message: '更新用户失败'
    })
  }
}) 